home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / greasemonkey-0.8.20080609.0-fx.xpi / chrome / greasemonkey.jar / chromeFiles / content / pages-overlay.js < prev    next >
Text File  |  2008-06-09  |  5KB  |  143 lines

  1. function PagesControl(ctlPages) {
  2.   var includesBox = new PagesBox(document.getElementById("grpIncluded"));
  3.   var excludesBox = new PagesBox(document.getElementById("grpExcluded"));
  4.  
  5.   this.notifyEvent = function(script, event, data) {
  6.     switch (event) {
  7.     case "edit-include-add": includesBox.pageAdded(data); break;
  8.     case "edit-include-remove": includesBox.pageRemoved(data); break;
  9.     case "edit-exclude-add": excludesBox.pageAdded(data); break;
  10.     case "edit-exclude-remove": excludesBox.pageRemoved(data); break;
  11.     }
  12.   };
  13.  
  14.   this.script = null;
  15.   this.populate = function(script) {
  16.     this.clear();
  17.     includesBox.populate(script, "includes", script.includes);
  18.     excludesBox.populate(script, "excludes", script.excludes);
  19.     this.script = script;
  20.     GM_getConfig().addObserver(this, this.script);
  21.   };
  22.  
  23.   this.clear = function() {
  24.     if (this.script == null) return;
  25.     GM_getConfig().removeObserver(this, this.script);
  26.     includesBox.clear();
  27.     excludesBox.clear();
  28.     this.script = null;
  29.   };
  30.  
  31.   function PagesBox(grpBox) {
  32.     var buttons = grpBox.getElementsByTagName("button");
  33.     var self = this;
  34.     var selectedPage = null;
  35.  
  36.     this.script = null;
  37.     this.type = null;
  38.     this.groupbox = grpBox;
  39.     this.listbox = grpBox.getElementsByTagName("listbox")[0];
  40.     this.btnAdd = buttons[0];
  41.     this.btnEdit = buttons[1]; 
  42.     this.btnRemove = buttons[2];
  43.  
  44.     this.listbox.addEventListener("select", updatePagesBox, true);
  45.     this.btnAdd.addEventListener("command", promptForNewPage, true);
  46.     this.btnEdit.addEventListener("command", promptForEdit, true);
  47.     this.btnRemove.addEventListener("command", remove, true);
  48.  
  49.     this.populate = function(script, type, pages) {
  50.       this.clear();
  51.       this.script = script;
  52.       this.type = type;
  53.  
  54.       for (var i = 0, page = null; (page = pages[i]); i++) {
  55.         addPage(page);
  56.       }
  57.     };
  58.  
  59.     this.clear = function() {
  60.       this.script = null;
  61.       this.type = null;
  62.  
  63.       while (this.listbox.hasChildNodes()) {
  64.         this.listbox.removeChild(this.listbox.childNodes[0]);
  65.       }
  66.     };
  67.  
  68.     function updatePagesBox(ev) {
  69.       selectedPage = self.listbox.getSelectedItem(0);
  70.       self.btnEdit.disabled = selectedPage == null;
  71.       self.btnRemove.disabled = selectedPage == null;
  72.     }
  73.  
  74.     function promptForNewPage(ev) {
  75.       var gmManageBundle = document.getElementById("gm-manage-bundle");
  76.       var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  77.                   .getService();
  78.       var wmi = wm.QueryInterface(Components.interfaces.nsIWindowMediator);
  79.       var win = wmi.getMostRecentWindow("navigator:browser");
  80.       var currentSite = GM_isGreasemonkeyable(win.content.location.href)
  81.                         ? win.content.location.protocol + "//" +
  82.                           win.content.location.hostname + "/*"
  83.                         : gmManageBundle.getString("promptForNewPage.defVal");
  84.       var val = gmPrompt(
  85.         gmManageBundle.getString("promptForNewPage.msg"),
  86.         currentSite,
  87.         gmManageBundle.getString("promptForNewPage.title"));
  88.  
  89.       if (val && val != "") {
  90.         self.type == "includes" ?
  91.           self.script.addInclude(val):
  92.           self.script.addExclude(val);
  93.         dirty = true;
  94.       }
  95.     }
  96.  
  97.     function promptForEdit(ev) { 
  98.       var gmManageBundle = document.getElementById("gm-manage-bundle"); 
  99.       var val = gmPrompt(
  100.         gmManageBundle.getString("promptForEdit.msg"),
  101.         self.listbox.selectedItem.label,
  102.         gmManageBundle.getString("promptForEdit.title"));
  103.  
  104.       if (val && val != "") {
  105.         self.type == "includes" ?
  106.           self.script.removeIncludeAt(self.listbox.selectedIndex):
  107.           self.script.removeExcludeAt(self.listbox.selectedIndex);
  108.         self.type == "includes" ?
  109.           self.script.addInclude(val):
  110.           self.script.addExclude(val);
  111.  
  112.         dirty = true; 
  113.       }
  114.     };
  115.  
  116.     this.pageAdded = function(val) {
  117.       addPage(val);
  118.     };
  119.  
  120.     function remove(ev) {
  121.       self.type == "includes" ?
  122.         self.script.removeIncludeAt(self.listbox.selectedIndex):
  123.         self.script.removeExcludeAt(self.listbox.selectedIndex);
  124.  
  125.       // it's sorta wierd that the button stays focused when it is disabled because nothing is selected
  126.       if (self.listbox.length == 0) {
  127.         self.listbox.focus();
  128.         dirty = true;
  129.       }
  130.     }
  131.  
  132.     this.pageRemoved= function(index) {
  133.       self.listbox.removeChild(self.listbox.childNodes[index]);
  134.     };
  135.  
  136.     function addPage(pageSpec) {
  137.       var listitem = document.createElement("listitem");
  138.       listitem.setAttribute("label", pageSpec);
  139.       self.listbox.appendChild(listitem);
  140.     }
  141.   }
  142. }
  143.